<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login_model extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
    public function validate(){
        // grab user input
        $user = $this->security->xss_clean($this->input->post('username'));
        $pass = $this->security->xss_clean($this->input->post('password'));
		// Prep the query
        $this->db->where('user', $user);
        $this->db->where('pass', do_hash($pass, 'md5'));
        // Run the query
        $query = $this->db->get('users');
        // Let's check if there are any results
        if($query->num_rows == 1) {
            $row = $query->row();
			if ($row->status == 0)
				return false;
            $data = array(
                    'id' => $row->id,
                    'f_name' => $row->f_name,
                    'l_name' => $row->l_name,
                    'user' => $row->user,
                    'email' => $row->email,
					'role' => $row->role
                );
            $this->session->set_userdata(array('validated' => true, 'currUser' => $data));
			$this->user_model->addUser_login_time($row->id);
            return true;
        }
        return false;
    }
}
?>